import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@MultipartConfig //必须加这个注解，开启对上传文件的支持,否则 调用 getPart()就会抛异常
@WebServlet("/upload")
public class UpLoadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 得到文件
        Part part = req.getPart("myImage");
        part.write("e:/aaa.png");// 写到磁盘中

        resp.setContentType("text/text; charset=utf8");
        resp.getWriter().write("上传成功!");
    }
}
